<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreate" />
    </div>
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="链接名" prop="name" align="center">
        <template slot-scope="scope">
          <a :href="scope.row.url">
            <img
              v-if="scope.row.favicon"
              style="border-radius:50%;width: 20px; height: 20px;"
              :src="scope.row.favicon"
            >
            <span>{{ scope.row.name }}</span>
          </a>
        </template>
      </el-table-column>
      <el-table-column label="链接介绍" prop="description" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.description }}</span>
        </template>
      </el-table-column>
      <el-table-column label="友链站长邮箱" prop="email" align="center">
        <template slot-scope="scope">
          <a v-if="scope.row.email" :href="`mailto:${scope.row.email}`"><i class="fa fa-envelope-o fa-fw" /></a>
          <span v-else>--</span>
        </template>
      </el-table-column>
      <el-table-column label="友链站长QQ" prop="qq" align="center">
        <template slot-scope="scope">
          <a v-if="scope.row.qq" :href="`tencent://message/?uin=${scope.row.qq}&Site=luoyuanxiangvip.com&Menu=yes`">
            <i class="fa fa-qq" />
          </a>
          <span v-else>--</span>
        </template>
      </el-table-column>
      <el-table-column label="来源" prop="source" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.source }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" prop="remark" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.remark }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="status" align="center">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status ? '' : 'danger'">{{ statusMap[scope.row.status] }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleUpdate(row)">
            <i class="fa fa-pencil fa-fw" />
          </el-button>
          <el-button size="mini" type="danger" @click="handleDelete(row)">
            <i class="fa fa-trash-o fa-lg" />
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="150px" style="width: 400px; margin-left:50px;">
        <el-form-item label="链接名" prop="name">
          <el-input v-model="temp.name" />
        </el-form-item>
        <el-form-item label="链接地址" prop="url">
          <el-input v-model="temp.url" />
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="temp.description" />
        </el-form-item>
        <el-form-item label="友链站长QQ" prop="qq">
          <el-input v-model="temp.qq" />
        </el-form-item>
        <el-form-item label="友链站长邮箱" prop="email">
          <el-input v-model="temp.email" />
        </el-form-item>
        <el-form-item label="图标" prop="favicon">
          <el-input v-model="temp.favicon" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="temp.remark" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-switch v-model="temp.status" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          确定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { feachList, saveOrUpdate, del } from '@/api/links'
import Pagination from '@/components/Pagination'
import { MessageBox } from 'element-ui'
export default {
  name: 'Links',
  components: {
    Pagination
  },
  data() {
    return {
      listLoading: false,
      tableKey: 0,
      total: 0,
      list: [],
      statusMap: {
        true: '启用',
        false: '禁用'
      },
      listQuery: {
        page: 1,
        limit: 10
      },
      dialogStatus: '',
      dialogFormVisible: false,
      textMap: {
        update: '修改',
        create: '添加'
      },
      temp: {},
      rules: {
        name: [{ required: true, message: '名称不能为空', trigger: 'change' }],
        url: [{ required: true, message: '链接地址不能为空', trigger: 'change' }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      this.listLoading = true
      const { result } = await feachList(this.listQuery)
      this.listLoading = false
      this.list = result.list
      this.total = result.total
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleCreate() {
      this.temp = {}
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    message(title, type, message) {
      this.$notify({
        title: title,
        message: message,
        type: type,
        duration: 2000
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          saveOrUpdate(this.temp).then(res => {
            if (res.code === 0) {
              this.message('添加友链', 'success', res.message)
              this.dialogFormVisible = false
              this.handleFilter()
            }
          })
        }
      })
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          saveOrUpdate(this.temp).then(res => {
            if (res.code === 0) {
              this.message('修改友链', 'success', res.message)
              this.dialogFormVisible = false
              this.handleFilter()
              this.$store.dispatch('tag/getTagList')
            }
          })
        }
      })
    },
    handleDelete(row) {
      MessageBox.confirm(`是否确认删除【${row.name}】这条数据？`, '删除', {
        confirmButtonText: '删除',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        del(row.id).then(res => {
          if (res.code === 0) {
            this.message('删除友链', 'success', res.message)
            this.dialogFormVisible = false
            this.handleFilter()
          }
        })
      })
    }
  }
}
</script>
